package com.breez.aiauth.core.aiface;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.FaceVerifyRequest;
import com.baidu.aip.face.MatchRequest;
import com.breez.aiauth.domain.FaceRequest;
import com.breez.aiauth.domain.GroupRequest;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AiAuthServiceImpl implements AiAuthService {
    @Autowired
    private AipFace client;


    @Override
    public JSONObject search(FaceRequest face) {
        return client.search(face.getImage(), face.getImageType(), face.getGroupIdList(), face.getOptions());
    }

    @Override
    public JSONObject detect(FaceRequest face) {
        return client.detect(face.getImage(), face.getImageType(), face.getOptions());
    }

    @Override
    public JSONObject addUser(FaceRequest face) {
        return client.addUser(face.getImage(), face.getImageType(), face.getGroupId(), face.getUserId(), face.getOptions());
    }

    @Override
    public JSONObject updateUser(FaceRequest face) {
        return client.updateUser(face.getImage(), face.getImageType(), face.getGroupId(), face.getUserId(), face.getOptions());
    }

    @Override
    public JSONObject faceDelete(FaceRequest face) {
        return client.faceDelete(face.getUserId(), face.getGroupId(), face.getFaceToken(), face.getOptions());
    }

    @Override
    public JSONObject getUser(FaceRequest face) {
        return client.getUser(face.getUserId(), face.getGroupId(), face.getOptions());
    }

    @Override
    public JSONObject faceGetlist(FaceRequest face) {
        return client.faceDelete(face.getUserId(), face.getGroupId(), face.getFaceToken(), face.getOptions());
    }

    @Override
    public JSONObject userCopy(FaceRequest face) {
        return client.userCopy(face.getUserId(), face.getOptions());
    }

    @Override
    public JSONObject deleteUser(FaceRequest face) {
        return client.deleteUser(face.getGroupId(), face.getUserId(), face.getOptions());
    }

    @Override
    public JSONObject match(String face1, String face2, String imageType) {
        MatchRequest req1 = new MatchRequest(face1, imageType);
        MatchRequest req2 = new MatchRequest(face2, imageType);
        List<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);
        return client.match(requests);
    }

    @Override
    public JSONObject faceverify(FaceRequest face) {
        FaceVerifyRequest req = new FaceVerifyRequest(face.getImage(), face.getImageType());
        ArrayList<FaceVerifyRequest> list = new ArrayList<FaceVerifyRequest>();
        list.add(req);
        return client.faceverify(list);
    }

    @Override
    public JSONObject groupDelete(GroupRequest group) {
        return client.groupDelete(group.getGroupId(), group.getOptions());
    }

    /**
     * 添加组
     *
     * @param group 组信息
     * @return
     */
    @Override
    public JSONObject groupAdd(GroupRequest group) {
        return client.groupAdd(group.getGroupId(), group.getOptions());
    }

    @Override
    public JSONObject getGroupList(GroupRequest group) {
        return client.getGroupList(group.getOptions());
    }

    @Override
    public JSONObject getGroupUsers(GroupRequest group) {
        return client.getGroupUsers(group.getGroupId(), group.getOptions());
    }
}
